﻿using AppDataStruct;
using ComLib.ComType;
using KWindow.KPageCore;
using LogLib;
using SoftKeyBoard;
using System;
using System.Windows.Forms;

namespace IPCPages
{
    public partial class PageReadIDCard : PageControlBase
    {
        private IDCardAll _cardInfo = null;

        private PictureBox pictureBox1 = null;

        private Label labelInputTip = null;

        private Button buttonInputID = null;

        private ProcessReadIDCard _process = null;

        public PageReadIDCard()
        {
            InitializeComponent();
            _process = ProcessReadIDCard.Instance;
            SetProcessBase(_process);
        }

        //初始化窗体数据
        public override void InitCtrls()
        {
            base.InitCtrls();
            pictureBox1 = GetControl<PictureBox>("pictureBox1") as PictureBox;
            labelInputTip = GetControl<Label>("labelInputTip") as Label;
            buttonInputID = GetControl<Button>("buttonInputID") as Button;
            SetButtonDefaultStyles(buttonCancel);

            //labelInputTip.Parent = pictureBox1;
            //buttonInputID.Parent = pictureBox1;           
            //countdownControl1.Parent = pictureBox1;           
            //buttonNext.Parent = pictureBox1;
            //buttonCancel.Parent = pictureBox1;
            //buttonBack.Parent = pictureBox1;
        }

        /// <summary>
        /// 保存身份证信息，用于预受理
        /// </summary>
        //private void SetSFZData()
        //{
        //    SFZInfo info = new SFZInfo();
        //    info.Address = _cardInfo.address;
        //    info.BirthDate = _cardInfo.birthday;
        //    info.ExpiryDate = _cardInfo.validtermOfStart + "-" + _cardInfo.validtermOfEnd;
        //    info.HeaderPicturePath = _cardInfo.HeadImage;
        //    info.IsManualInputNumberVerify = _cardInfo.IsManualInputNumberVerify;
        //    info.IssueUnit = _cardInfo.signdate;
        //    info.Name = _cardInfo.name;
        //    info.Nation = _cardInfo.people;
        //    info.Sex = _cardInfo.sex;
        //    info.SFZH = _cardInfo.number;
        //    Process.SetValue("SFZInfo", info);
        //}

        /// <summary>
        /// 保存窗体数据(窗体本次做完后, 进入下一步之前)
        /// </summary>
        public override void UpdateData()
        {
            base.UpdateData();
        }

        /// <summary>
        /// 清理窗体数据(窗体本次做完后)
        /// </summary>
        public override void ClearData()
        {
            base.ClearData();
            _cardInfo = null;
        }

        /// <summary>
        /// 窗体关闭时
        /// </summary>
        public override void Close()
        {

        }

        public override void StartWork()
        {
            base.StartWork();
            Speak("请把本人身份证放置于身份证阅读区");
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        public override void OnCountdownCompleted(object sender, EventArgs e)
        {
            StopWork();
            if (Process.Job != null)
            {
                Process.Job.Cancel("倒计时结束，读取身份证操作未完成，取消拍照.");
            }
            base.OnCountdownCompleted(sender, e);
        }

        private void buttonInputID_Click(object sender, EventArgs e)
        {
            string inputIDNumber = "";
            StopWork();
            if (StartManualInput(ref inputIDNumber))
            {
                _cardInfo = new IDCardAll();
                _cardInfo.number = inputIDNumber;
                _cardInfo.birthday = IDCardCheck.GetBirthday(inputIDNumber);
                _cardInfo.sex = IDCardCheck.GetSex(inputIDNumber);
                _cardInfo.IsManualInputNumberVerify = true;
                _process.IDCardInfo = _cardInfo;
                Process.UpdateData();
                //Process.ToAny("IPCPages.PageNeedAttention");
                Process.ToNext();
            }
            else
            {
                StartWork();
            }
        }

        private bool StartManualInput(ref string inputStringValue)
        {
            bool bRet = false;
            Log.WriteAsync("用户启动了身份证人工输入操作...");
            FormInputID formInputID = new FormInputID();
            Speak("请输入您的身份证号码");
            if (formInputID.ShowDialog() == DialogResult.OK)
            {
                inputStringValue = formInputID.Number;
                bRet = true;
            }
            else
            {
                Log.WriteAsync("用户取消了人工输入...");
                bRet = false;
            }
            return bRet;
        }

        protected override void btnCancel_Click(object sender, EventArgs e)
        {
            StopWork();
            base.btnCancel_Click(sender, e);
        }
    }
}
